# -*- coding: utf-8 -*-
# @Time    : 2023/3/17 18:33
# @Author  : Flora.Chen
# @File    : conftest.py
# @Software: PyCharm
# @Desc:

# 标准库导入
import os
# 第三方库导入
import pytest
from playwright.sync_api import Browser
from loguru import logger
# 本地应用/模块导入
from config.path_config import INTERFACE_DIR
from config.global_vars import GLOBAL_VARS
from utils.base_utils.request_control import RequestControl


@pytest.fixture(scope="session", autouse=True)
def reset_login_times(browser: Browser, base_url):
    logger.info("\n-------------- Start: 开启测试前的操作 ----------------")
    # 超级管理远账号
    users = {
        "login": GLOBAL_VARS['admin_user_login'],
        "password": GLOBAL_VARS['admin_user_password'],
        "user_id": GLOBAL_VARS["admin_user_id"]
    }

    # 手动创建一个新的APIRequest上下文实例
    api_request_context = browser.new_context(base_url=base_url).request

    # # 发送登录请求, api_request_context会自动存储登录态，下一个请求会自动带上登录态
    RequestControl(api_request_context=api_request_context).api_request_flow(
        api_file_path=os.path.join(INTERFACE_DIR, "gitlink_login.yaml"), key="gitlink_login", global_var=users)

    # -------- 通过调用API，恢复禁密账号，保证账号未被锁定 -----------
    for key, value in GLOBAL_VARS.items():
        if "user_id" in key and value != users["user_id"]:
            RequestControl(api_request_context=api_request_context).api_request_flow(
                api_file_path=os.path.join(INTERFACE_DIR, "admins", "user_manage_api.yaml"),
                key="reset_login_times",
                global_var={"user_id": value})
    logger.info("\n-------------- End: 开启测试前的操作 ----------------")
